package dk;

import java.util.ArrayList;
import java.util.List;

public class fileInfo {
	
	private String fileName;
	private List<String> rawfileContents=new ArrayList<String>();
	private List<String> cleanfileContents=new ArrayList<String>();
	private List<methodInfo> methodList = new ArrayList<methodInfo>();
	
	private String rawFilePath = "";
	private String modifiedFilePath = "";
	private String modifiedFilePathNoClassName = "";
	
	
	util u = new util();
	
	
	public List<methodInfo> getMethodList() {
		return methodList;
	}

	public void setMethodList(List<methodInfo> methodList) {
		this.methodList = methodList;
	}
	
	
	public String getFileName() {
		return fileName;
	}
	
	public String getmodifiedFilePathNoClassName() {
		return modifiedFilePathNoClassName;
	}
	
	public fileInfo(String fileName) {
		this.fileName = fileName;
	}

	
	public void addFileContents(String lineToAdd){
		
		rawfileContents.add(lineToAdd);
		cleanfileContents.add(cleanRow(lineToAdd));	
	}
	
	
	public String getAllRawFileContents(){
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<rawfileContents.size(); i++){
			sb.append(rawfileContents.get(i) + "\n");
		}
		return sb.toString();
	}
	
	public String getAllCleanFileContents(){
		StringBuilder sb = new StringBuilder();
		for (int i=0; i<cleanfileContents.size(); i++){
			sb.append(cleanfileContents.get(i) + "\n");
		}
		return sb.toString();
	}
	
	
	// check to see if the file contents should be cleaned?
	
	private  String cleanRow(String rowInput){
		rowInput = rowInput.toLowerCase();
	//	System.out.println("RI" + rowInput);
		String retVal="";
		if(rowInput.toLowerCase().contains("crest")){
			// replace all of the numbers with xxx - These values are not needed
			retVal = rowInput.replaceAll("[0-9+]", "!@");
		}else{
			retVal = rowInput;
		}
	
		
		
		
		
		retVal= removeCrest(retVal);
		retVal = removeVariableNames(retVal);
	
		return retVal;
	}
	
	
	// Create the necessary method information
	// This can surely be made better in the future.
	public void createMethodGroups(){
		
		rawFilePath = "";
		

//		final String Seperator = "*^*^*^*^*^*^*^*^*^*^*^";
		final String Seperator = "yesmethod";
		StringBuilder sb = new StringBuilder();
		boolean isSeperatorFound = false;
		
		// create a temp list to hold the strings
		List<String> tempList=new ArrayList<String>();
		
		for(int i=0; i<cleanfileContents.size();i++){
					

	
			if (rawFilePath.equals("")){ // only do this check if the file path has not been determined
				if((cleanfileContents.get(i).contains("#line") && cleanfileContents.get(i).contains("*^yesmethod") && cleanfileContents.get(i).contains(".c"))){
			//		System.out.println("********** line:" + cleanfileContents.get(i));	
	// find everything between first " and first .c
					
					String temp = cleanfileContents.get(i);
					temp = temp.substring(temp.indexOf("\"")+1, temp.indexOf(".c"))+".c";
					
		//			System.out.println("***********************************" + temp);
					rawFilePath = 	temp;	
					modifiedFilePath = u.convertRawMethodToReportFriendly(rawFilePath);
					modifiedFilePathNoClassName = u.RemoveMethodNameFromPath(modifiedFilePath);

				}
			}
			
			
			
			
			// Things can start being recorded
			if (isSeperatorFound==true){
				if(!cleanfileContents.get(i).startsWith("#")){	// Ignore lines with comments
					sb.append(cleanfileContents.get(i)).append("\n").toString().replaceAll("^\\s+", "");
				}
			}
			
			
			// Do not start recording any information until the seperator is discovered
			if(cleanfileContents.get(i).contains(Seperator)){
		//		System.out.println("Sep: " + cleanfileContents.get(i));
				isSeperatorFound=true;
			}
		
			
			if (isSeperatorFound==false){
		//		System.out.println("Ignore: " + cleanfileContents.get(i));
			//	sb.append(cleanfileContents.get(i)).append("\n");
			}
			
			
			if ((isSeperatorFound==true) && (cleanfileContents.get(i).contains(Seperator))){
			//	System.out.println("New section" + cleanfileContents.get(i));
				tempList.add(sb.toString());
				sb.setLength(0); // clear the strinbBuilder
			}			
			
			if(i == cleanfileContents.size()-1){
				//System.out.println("end" + cleanfileContents.get(i));
				tempList.add(sb.toString());
			}
		}
			
		
		for(int a=0; a<tempList.size();a++){
			if(tempList.get(a).length()>0){ // Make sure that there are values to record
				if(isSectionGoodMethodInfo(tempList.get(a))){
					//methodList.add(new methodInfo(u.getMethodName(tempList.get(a).split("\n")[1]),tempList.get(a)));
					methodList.add(new methodInfo(u.getMethodName(tempList.get(a).split("\n")[1]),tempList.get(a),rawFilePath,modifiedFilePath));
				}
			}
		}
		
	//	System.out.println(methodList.size());
	}
	
	
	// Check the entire section contents to see if it is actually a "Good" section
	private boolean isSectionGoodMethodInfo(String inputSection){
		
		boolean retVal=false;
		String[] myArray = inputSection.split("\n");
	if(myArray.length >2){
		if(((!myArray[2].contains("__")))){
			if((inputSection.contains("{"))){
	
				retVal=true;
			}
		}
	}
		return retVal;
	}
	
	//Remove lines that start with __crest
	private String removeCrest(String input){
		String retVal="";
		
		if(!input.trim().toLowerCase().startsWith("__crest")){
			//System.out.println("here" + input);
			retVal=input;
		}
		return retVal;
	}
	
	
	private String removeVariableNames(String input){
		String retVal=input;
		final String seperator = "#@#rv#@#";
		if(input.toLowerCase().contains(seperator)){
			String temp;
			temp = u.reverseString(input);
			temp = u.reverseString(temp.substring(0, temp.indexOf(" ")));
			temp = temp.replace(seperator, "");
			
			retVal = input.replace(temp, "x;").replace(seperator, "");
		}
		
		return retVal+"";
	}
	
	
	public String getRawFilePath() {
		return rawFilePath;
	}


	
	// When this is set, generate the value of the modified output path.
	public void setRawFilePath(String rawFilePath) {
		this.modifiedFilePath = "temp";
		this.rawFilePath = rawFilePath;
	}


	public String getModifiedFilePath() {
		return modifiedFilePath;
	}


	public void setModifiedFilePath(String modifiedFilePath) {
		this.modifiedFilePath = modifiedFilePath;
	}

	
}
